<div class="layui-fluid">
	<div class="layui-row">
		<div class="layui-col-xs12">

			<table id="demo_hash" lay-filter="type_hash" lay-even lay-skin="nob"></table>
			<script type="text/html" id="barDemo_hash">
				<a class="layui-btn" lay-event="edit">编辑</a>
								<a class="layui-btn" lay-event="look">详情</a>
								<a class="layui-btn" lay-event="del">删除</a>
       </script>

		</div>
	</div>
</div>
<script type="text/html" id="toolbarDemo">
	<div class="toobarbox">
	<div class="layui-btn-container" >
    <button class="layui-btn " lay-event="addbtn">添加交流</button>
    <!-- <button class="layui-btn layui-btn-primary" lay-event="delallbtn">删除选中</button> -->
  </div>

		<div class="layui-inline">
			<input type="text" class="layui-input" id="dataxx" placeholder="选择日期">
		</div>
		<div class="tablesearch iconfont iconsousuo ">
<input type="text" class="layui-input " id="tablesearchid" title="请输入搜索的关键字" placeholder="请输入搜索关键字">
		</div>
			
			
			</div>
</script>

<script type="text/html" id="userStatusTp">
	<!-- 这里的 checked 的状态只是演示 -->
  <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch"  lay-filter="userDemo" {{ d.status == 1 ? 'checked' : '' }}>
</script>


<script>
	var xxjson = {
		search: '',
		startTime: '',
		stopTime: ''
	};
	layui.config({
		base: '/src/js/'
	}).use(['jquery', 'mockjs', 'table', 'sidebar', 'form', 'laydate'], function() {
		var $ = layui.jquery,
			layer = layui.layer,
			table = layui.table,
			sidebar = layui.sidebar,
			form = layui.form,laydate = layui.laydate;
		var tablesearchidval = ''
		//第一个实例
		var typetable = table.render({
			size: 'lg',
			id: 'usertableid',
			where: xxjson,
			contentType: 'application/json',
			pageSize: 20,
			elem: '#demo_hash',
			toolbar: '#toolbarDemo',
				height: 'full-100',
			url: HH_URL + MsjlGet, //数据接口
			page: true, //开启分页
			even: true,
			skin: 'nob',
			cellMinWidth: 100,
			cols: [
				[ //表头
					{
						type: 'numbers',
						fixed: 'left'
					}, {
						field: 'cover',
						title: '课程封面',
						width: 120,
						templet: function(d) {
							if (d.cover == '') {
								return ''
							} else {
								return '<img style="height: 100%;" src="' + IMG_URL + d.cover + '" class="lookimgcss" > '
							}

						}
					},
					{
						field: 'id',
						title: 'ID',
						width: 50,
						hide: true
					}, {
						field: 'title',
						title: '课程标题',
						width: 400
					}, {
						field: 'realName',
						title: '讲师',
						width: 120
					},
					{
						field: 'tbProblemContent',
						title: '添加内容'
					}, {
						field: 'praiseNumber',
						title: '点赞数量',
						width: 120
					}, {
						title: '操作',
						fixed: 'right',
						width: 180,
						toolbar: '#barDemo_hash'
					}
				]
			],

			done: function(res, curr, cont) {
				laydate.render({
					elem: '#dataxx'	, //指定元素
					range: true,
					done: function(value, date, endDate) {
						if (date.year == undefined) {
							xxjson.startTime = '';
							xxjson.stopTime = '';
						} else {
							xxjson.startTime = date.year + '-' + date.month + '-' + date.date;
							xxjson.stopTime = endDate.year + '-' + endDate.month + '-' + endDate.date;
						}
						typetable.reload({
							where: xxjson,
							page: {
								curr: 1 //重新从第 1 页开始
							}
						});
						cshfn()
					}
				});
				
				$('#tablesearchid').keydown(function(event) {
					tablesearchidval = $('#tablesearchid').val()
					xxjson.search = tablesearchidval;
					if (event.keyCode == 13) {
						typetable.reload({
							where: xxjson,
							page: {
								curr: 1 //重新从第 1 页开始
							}
						});
						cshfn()
					}
				})

			}
		});
		function cshfn() {
			$('#tablesearchid').val(tablesearchidval);
			if (xxjson.startTime == null || xxjson.startTime == '') {
				$('#dataxx').val('');
			} else {
				$('#dataxx').val(xxjson.startTime + ' - ' + xxjson.stopTime);
			}
		
			// $('#dataxx').val(xxjson.startTime + ' - ' + xxjson.stopTime)
		}

		form.on('switch(userDemo)', function(obj) {
			// 监听用户的状态替换
			if (obj.elem.checked) {
				var checknum = 1
			} else {
				var checknum = 0
			}
			POSTAJAX(UserModStatus, {
				note: '',
				tbId: obj.value,
				tbType: checknum
			}, function(res) {
				if (res.status == 200) {
					layer.msg("状态修改成功", {
						"icon": 1
					});
				}
			})
		});




		//监听事件
		table.on('toolbar(type_hash)', function(obj) {
			var checkStatus = table.checkStatus(obj.config.id);
			var data = obj.data; //获得当前行数据
			var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
			var tr = obj.tr; //获得当前行 tr 的DOM对象
			switch (layEvent) {
				case 'delallbtn':
					//批量删除行数
					// console.log(checkStatus.data);
					var tbarray = [];
					$.each(checkStatus.data, function(idx, obj) {
						tbarray.push(obj.tbId);
					})

					layer.confirm('确认删除所选中行的数据吗?', function(index) {
						POSTAJAX(VtypeDel, {
							ids: tbarray
						}, function(res) {
							layer.close(index);
							layer.msg(res.msg);
							if (res.status == 200) {
								typetable.reload('demo_hash');
							}
						})
						//向服务端发送删除指令
					});

					break;
				case 'addbtn':
					location.href = "#/teachersexchange/det"
					break;
				default:

					break;
			}
		});

		//监听工具条
		table.on('tool(type_hash)', function(obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
			var data = obj.data; //获得当前行数据
			var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
			var tr = obj.tr; //获得当前行 tr 的DOM对象
			switch (layEvent) {
				case 'look':
					location.href = '#/teachersexchange/one?tbId=' + obj.data.tbId;
					break;
				case 'del':
					delfn({
						tbId: obj.data.tbId
					}, obj);

					break;
				case 'edit':
							location.href = '#/teachersexchange/det?tbId=' + obj.data.tbId;
					break;

				default:

					break;
			}

		});




		function delfn(tdata, obj) {
			layer.confirm('确认删除该名师交流吗?', function(index) {
				POSTAJAX(MsjlDel, tdata, function(res) {
					layer.close(index);
					layer.msg(res.msg);
					if (res.status == 200) {
						obj.del(); //删除对应行（tr）的DOM结构，并更新缓存

					}

				})

				//向服务端发送删除指令
			});
		}


		function lookfn(Ttitle, placeholdertxt, value, callback) {
			// 查看类型名称进行修改/添加
			layer.prompt({
					title: Ttitle,
					formType: 0,
					value: value,
					tiptxt: '请文明' + Ttitle,
					btntips: '一经发现违法，平台有权对其删除',
					placeholdertxt: placeholdertxt,
					btn: [Ttitle],
				},
				function(pass, index) {
					layer.close(index);
					callback(pass, index)
				});
		}

		$('#search_hash').on('click', function() {
			var that = this;
			// console.log($('#search_tpl_hash').html());
			sidebar.render({
				elem: that,
				content: $('#search_tpl_hash').html(),
				title: '搜索',
				shade: true,
				// shadeClose:false,
				// direction: 'left'
				// dynamicRender: false,
				// url: 'views/member/all/search.html',
				width: '500px', //可以设置百分比和px
				done: function() {
					console.log('ccc');
					form.render();

					//监听提交
					form.on('submit(formDemo_hash)', function(data) {
						// layer.msg(JSON.stringify(data.field));
						table.reload('demo_hash', {
							where: data.field
						});
						return false;
					});
				}
			});
		});
	});
</script>

<style scoped>

</style>
